package com.example.buildtable.controller;

import com.example.common.util.R;
import com.example.buildtable.pojo.BankCard;
import com.example.buildtable.service.BankCardService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@Tag(name = "银行卡")
@RequestMapping(value = "/bankCard/")
public class BankCardController {
    @Autowired
    private BankCardService bankCardService;
    
    @Operation(summary = "查询所有银行卡数据")
    @GetMapping(value = "/getAllBankCard")
    public R getAllBankCard() {
        List<BankCard> bankCardList = bankCardService.list();
        return R.ok("查询所有银行卡数据成功").data("bankCardList", bankCardList);
    }
    
    @Operation(summary = "增加银行卡")
    @PostMapping(value = "/addBankCard")
    public R addBankCard(BankCard bankCard) {
        boolean flag = bankCardService.save(bankCard);
        if (flag) {
            return R.ok("增加银行卡成功");
        } else {
            return R.ok("增加银行卡失败");
        }
    }

    @Operation(summary = "删除银行卡")
    @GetMapping(value = "/deleteBankCard/{backCardId}")
    public R deleteBankCard(@PathVariable("backCardId") Integer backCardId) {
        boolean flag = bankCardService.removeById(backCardId);
        if (flag) {
            return R.ok("删除银行卡成功");
        } else {
            return R.ok("删除银行卡失败");
        }
    }

    @Operation(summary = "按照银行卡查看")
    @GetMapping(value = "/getBankCardById/{backCardId}")
    public R getBankCardById(@PathVariable("backCardId") Integer backCardId) {
        BankCard bankCard = bankCardService.getById(backCardId);
        if (bankCard != null) {
            return R.ok("按照银行卡查看成功").data("bankCard", bankCard);
        } else {
            return R.error("按照银行卡查看失败");
        }
    }

    @Operation(summary = "修改银行卡")
    @PostMapping(value = "/updateBankCard")
    public R updateBankCard(BankCard bankCard) {
        boolean flag = bankCardService.updateById(bankCard);
        if (flag) {
            return R.ok("修改银行卡成功");
        } else {
            return R.ok("修改银行卡失败");
        }
    }
}